package com.example.zhaoping.controller;

import com.auth0.jwt.JWT;
import com.example.zhaoping.entity.Admin;
import com.example.zhaoping.entity.Enterprise;
import com.example.zhaoping.entity.User;
import com.example.zhaoping.serviceImpl.AdminServiceImpl;
import com.example.zhaoping.serviceImpl.EnterpriseServiceImpl;
import com.example.zhaoping.serviceImpl.UserServiceImpl;
import com.example.zhaoping.util.Result;
import com.example.zhaoping.util.TokenUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Resource
    private UserServiceImpl userServiceImpl;
    @Resource
    private EnterpriseServiceImpl enterpriseService;
    @Resource
    private AdminServiceImpl adminService;

    private TokenUtil tokenUtil;

    private Result<Map<String, Object>> result = new Result<Map<String, Object>>();

    /**
     * 登录
     * @param user
     * @return 用户信息
     */
    @PostMapping(value = "/user/login", name = "用户登录")
    public Result<?> login(User user){
        if(userServiceImpl.selectByUserName(user) == null){return result.error("用户不存在！请前往注册");}
        User user1 = userServiceImpl.login(user);
        if(user1 != null){
            Map<String,Object> user_map = new HashMap<>();
            user_map.put("id",user1.getId());
            user_map.put("username",user.getUsername());
            user_map.put("name",user1.getName());
            user_map.put("avatar",user1.getAvatar());
            TokenUtil tokenUtil = new TokenUtil();
            String token1 =  tokenUtil.sign(user1.getId(),user_map);
            user_map.remove("username");
            user_map.put("token",token1);
            return result.success("登录成功！",user_map);
        }else {
            return result.error("账号或密码错误！");
        }
    }

    @PostMapping( value = "/admin/login",name = "管理员登录")
    public Result adminLogin(Admin admin){
        if(adminService.selectByUserName(admin) == null){return new Result<>(400,"该管理员不存在！请联系负责人",null);}
        Admin admin1 = adminService.adminLogin(admin);
        if(admin1 != null){
            Map<String,Object> admin_map = new HashMap<>();
            admin_map.put("username",admin.getUsername());
            TokenUtil tokenUtil = new TokenUtil();
            String token1 =  tokenUtil.sign(admin1.getId(),admin_map);
            admin_map.remove("username");
            admin_map.put("token",token1);
            return result.success("登录成功！",admin_map);
        }else {
            return result.error("账号或密码错误！");
        }
    }
    @PostMapping( value = "/enterprise/login",name = "企业登录")
    public Result<?> enterpriseLogin(Enterprise enterprise){
        if(enterpriseService.selectOne(enterprise.getUsername()) == null){return new Result<>(400,"该企业不存在！请前往注册",null);}
        Enterprise enterprise1 = enterpriseService.login(enterprise);
        if(enterprise1 != null){
            Map<String,Object> enterprise_map = new HashMap<>();
            enterprise_map.put("id",enterprise1.getId());
            enterprise_map.put("username",enterprise1.getUsername());
            enterprise_map.put("name",enterprise1.getName());
            enterprise_map.put("avatar",enterprise1.getAvatar());
            TokenUtil tokenUtil = new TokenUtil();
            String token1 =  tokenUtil.sign(enterprise1.getId(),enterprise_map);
            enterprise_map.remove("username");
            enterprise_map.put("token",token1);
            return result.success("登录成功！",enterprise_map);
        }else {
            return result.error("账号或密码错误！");
        }
    }

    @GetMapping(value = "/logout", name = "退出登录")
    public Result logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String u = JWT.decode(token).getAudience().get(0);
        return result.success("退出登录");
    }
}
